package bbalgae.swing;

import java.awt.Component;
import java.awt.PopupMenu;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.JButton;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.Popup;
import javax.swing.event.PopupMenuEvent;
import javax.swing.event.PopupMenuListener;

public class SwingPopMenu extends JPopupMenu {
	private JPopupMenu popup = null;

	public SwingPopMenu(JPanel p) {
		// 팝업메뉴 생성

		popup = new JPopupMenu();
		JMenuItem m1 = new JMenuItem("new");
		popup.add(m1);
		p.addMouseListener(new PopupMenuListener());
		// SwingMain의 contentpane(Jpanel)에 PopupmenuListener를 MouseListener로 등록
	}

	class PopupMenuListener extends MouseAdapter {

		@Override
		public void mousePressed(MouseEvent e) {
			if (e.isPopupTrigger()) {
				popup.show(e.getComponent(), e.getX(), e.getY());
			}
		}

		@Override
		public void mouseReleased(MouseEvent e) {
			if (e.isPopupTrigger()) {
				popup.show(e.getComponent(), e.getX(), e.getY());
			}
		}

		private void showMenu(MouseEvent e) {
			popup.show(e.getComponent(), e.getX(), e.getY());
		}

	}
}
